import { showNotification } from '@/utils/notify';
import request from '@/utils/request';
import { defineStore } from 'pinia';
import { ref } from 'vue';

type Student = {
    name: string,
    gender: string,
    major: string,
    enrollmentDate: Date,
    studentNumber: string
}

type StudentPage = {
    code: number, 
    message: string,
    data: {
        records: Student[],
        total: number,
        size: number, 
    }
}   

export const useStudentStore = defineStore('student', () => {
    const students = ref<Student[]>([]);
    const total = ref(0); // 总课程数量
    const currentPage = ref(1); // 当前页码
    const pageSize = ref(10); // 每页显示的课程数量


    const addStudent = async(student: Student) => {
        const { code, data } = await request.post<{ code: number; data: object }>('/api/students/add', student);
        if (code === 200) {
            showNotification("学生添加成功", false);
            fetchStudents()

        } else {
            showNotification("学生添加失败", true);
        }
    };

    const updateStudent = async(index: number, updatedStudent: Student) => {
        try {
            const studentNumber = updatedStudent.studentNumber;
            const { code, data } = await request.post<{ code: number; data: object }>(
                `/api/students/update/${studentNumber}`,
                updatedStudent
            );
            if (code === 200) {
                showNotification("学生更新成功", false);
                fetchStudents()

            } else {
                showNotification("学生更新失败", true);
            }
        } catch (error) {
            showNotification("学生更新失败", true);
        }
    };

    const deleteStudent = async(index: number) => {
        try {
            const studentNumber = students.value[index].studentNumber;
            const { code, data } = await request.post<{ code: number; data: object }>(
                `/api/students/remove/${studentNumber}`
            );
            if (code === 200) {
                showNotification("学生删除成功", false);
            } else {
               showNotification("学生删除成功", true);
            }
            fetchStudents()

        } catch (error) {
            showNotification("学生删除成功", true);
        }
    };

    const fetchStudents = async () => {
        try {
            const { data } = await request.get<StudentPage>(
                `/api/students/page?current=${currentPage.value}&size=${pageSize.value}`
            );
            students.value = data.records;
            total.value = data.total;
           showNotification( '获取学生信息成功',false); 

        } catch (error) {
           showNotification( '获取学生信息失败',true); 
        }
    }

    const changePage = (page: number) => {
        currentPage.value = page; // 更新当前页码
        fetchStudents(); // 重新获取课程列表
    };

    onMounted(() => {
        fetchStudents(); // 初始加载第一页 
    })

    return {
        students,
        addStudent,
        updateStudent,
        deleteStudent,
        changePage,
        currentPage,
        pageSize,
        total

    };
});